﻿using System.Linq;
using Identity;
using Identity.Searcher;
using JwtWebApi.Filters;
using JwtWebApi.Models.Manage;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;

namespace JwtWebApi.Controllers.Manage
{
    [Authorize]
    [Route("manage/[controller]")]
    [ApiController]
    [EnableCors]
    public class RoleController : Controller
    {
        private readonly RoleManager<Role> _userManager;

        public RoleController(RoleManager<Role> userManager)
        {
            _userManager = userManager;
        }

        [HttpGet("all")]
        public IActionResult All()
        {
            return Json(_userManager.Roles);
        }

        /// <summary>
        /// </summary>
        /// <param name="userSearch"></param>
        /// <returns></returns>
        [HttpGet("list")]
        public IActionResult Index([FromQuery] RoleSearch userSearch)
        {
            var result = _userManager.Roles
                .Where(user =>
                    user.Name == userSearch.Name || user.NormalizedName == userSearch.Name);

            return Json(new
            {
                data = result.Skip(userSearch.GetSkip()).Take(userSearch.PageSize ?? 30).Select(f => f)
                    .ToList(),
                total = result.Count()
            });
        }

        /// <summary>
        /// </summary>
        /// <param name="userModel"></param>
        /// <returns></returns>
        [HttpPost("save")]
        [ModelValidteFilter]
        public IActionResult Save([FromBody] RoleModel userModel)
        {
            return Json(new {code = 300, message = "成功保存"});
        }
    }
}